using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ChuanPlus.DAO;
using DevExpress.Data.Filtering;
namespace ChuanPlus.FormControl
{
    public partial class ControlProduct : DevExpress.XtraEditors.XtraUserControl
    {
        public ControlProduct()
        {
            InitializeComponent();
            comboDistributor.Properties.Items.AddRange(xpColDistributor);
            repositoryItemComboBox1.Items.AddRange(xpColDistributor);
        }
        public bool validation()
        {
            if (textItemCode.Text == "")
            {
                MessageBox.Show("Please enter Item Code","Message"); textItemCode.Focus(); return false;
            }            
            if (textDescription.Text == "")
            {
                MessageBox.Show("Please enter product description", "Message"); textDescription.Focus(); return false;
            }
            if (comboDistributor.Text == "")
            {
                MessageBox.Show("Please select warehouse", "Message"); comboDistributor.Focus(); return false;
            }
            if (textUnit.Text == "")
            {
                MessageBox.Show("Please enter Unit", "Message"); textUnit.Focus(); return false;
            }           
            if (textUnitPrice.Text == "")
            {
                MessageBox.Show("Please enter Unit Price", "Message"); textUnitPrice.Focus(); return false;
            }
            if (textRedInvQty.Text == "")
            {
                MessageBox.Show("Please enter Red invoice quantity", "Message"); textRedInvQty.Focus(); return false;
            }
            if (textTax.Text == "")
            {
                MessageBox.Show("Please enter Tax", "Message"); textTax.Focus(); return false;
            }
            if (ProductDAO.IsProductExsit(textItemCode.Text, ((Distributor)comboDistributor.SelectedItem).DistributorID))
            {
                MessageBox.Show("This Item Code is registed already", "Message"); textItemCode.Focus(); return false;
            }
            return true;
        }

        private void buttonInsert_Click(object sender, EventArgs e)
        {
            if (!validation()) return;
            Product p = new Product(textItemCode.Text, comboDistributor.SelectedItem as Distributor, textDescription.Text, textUnit.Text, Convert.ToDouble(textRedInvQty.Text), Convert.ToDouble(textUnitPrice.Text), Convert.ToDouble(textTax.Text), checkRowMaterial.Checked);
            p.Save();
            xpColProducts.Reload();
            MessageBox.Show("Product has been added to database", "Message");
            reset();
        }

        private void textRedInvNo_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "")
            { 
            
            }

        }
        private void reset()
        {
            textItemCode.Text = "";
            textDescription.Text = "";
            textRedInvQty.Text = "";
            comboDistributor.Text = "";
            textUnitPrice.Text = "";
            checkRowMaterial.Checked = false;
        }
        private void textFilter_EditValueChanged(object sender, EventArgs e)
        {
            CriteriaOperator opeLeft = new BinaryOperator(new OperandProperty("ItemCode"), new OperandValue("%" + textFilter.Text + "%"), BinaryOperatorType.Like);
            CriteriaOperator opeRight = new BinaryOperator(new OperandProperty("Description"), new OperandValue("%" + textFilter.Text + "%"), BinaryOperatorType.Like);
            CriteriaOperator filter = new GroupOperator(GroupOperatorType.Or, new CriteriaOperator[2] { opeLeft, opeRight });
            xpColProducts.Filter = filter;
        }

        private void buttonReset_Click(object sender, EventArgs e)
        {
            textItemCode.Text = textDescription.Text = textRedInvQty.Text = textUnitPrice.Text = "";
            comboDistributor.SelectedItem = null;
        }

        private void gridView1_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e)
        {
            int DSRowIndex = gridView1.GetDataSourceRowIndex(gridView1.FocusedRowHandle);
            Product p = xpColProducts[DSRowIndex] as Product;
            if (gridView1.FocusedColumn.FieldName == "ItemCode")
            {                
                int result = IsDuplicateItemCodeDistributor(e.Value.ToString(), p.DistributorID.DistributorID, DSRowIndex);
                if (result != -1)   // item code and distributor is duplicate
                {
                    e.Valid = false;
                    e.ErrorText = "This Item Code and distributor is duplicate";
                    int dupRow = gridView1.GetRowHandle(result); // get row handle from data source index
                    gridView1.FocusedRowHandle = dupRow;
                }
            }    
            if(gridView1.FocusedColumn.FieldName == "DistributorID!Key")
            {
                int result = IsDuplicateItemCodeDistributor(p.ItemCode,Convert.ToInt32(e.Value),DSRowIndex);
                if (result != -1)   // item code and distributor is duplicate
                {
                    e.Valid = false;
                    e.ErrorText = "This Item Code and distributor is duplicate";
                    int dupRow = gridView1.GetRowHandle(result); // get row handle from data source index
                    gridView1.FocusedRowHandle = dupRow;
                }
            }
        }
        private int IsDuplicateItemCodeDistributor(string itemcode, int distributor, int currentRow)
        {
            Product item;
            for(int i=0; i< xpColProducts.Count; i++)
            {
                if (i != currentRow)
                {
                    item = xpColProducts[i] as Product;
                    if (item.ItemCode == itemcode && item.DistributorID.DistributorID == distributor)
                        return i; 
                }
            }
            return -1;    
        }        
    }
}
